package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.m510.autosurfer.common.cli.OutDirOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.InternalState;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.smbcvisa.BillPendingException;
import net.m510.autosurfer.smbcvisa.SmbcVisaDownload;
import net.m510.autosurfer.smbcvisa.SmbcVisaGoDetail;
import net.m510.autosurfer.smbcvisa.SmbcVisaGoTop;
import net.m510.autosurfer.smbcvisa.SmbcVisaLogin;
import net.m510.autosurfer.smbcvisa.SmbcVisaLoginChecker;
import net.m510.autosurfer.smbcvisa.SmbcVisaLogout;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.nodes.Document;

public class SmbcVisaGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(SmbcVisaGetMain.class.getName());
	
	private String userId;
	private String password;
	private File outDir;

	/**
	 * 
	 */
	public SmbcVisaGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SmbcVisaGetMain main = new SmbcVisaGetMain();
		main.setLogger(log);
		main.Main(args);
	}

	@Override
	protected Document start() throws IOException, UserProcessException {
		SmbcVisaGoTop p = new SmbcVisaGoTop(client,context);
		Document out = p.process(null);
		p.check(out);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException,UserProcessException {
		SmbcVisaLogin p = new SmbcVisaLogin(client,context);
		p.setUserId(userId);
		p.setPassword(password);
		Document out = p.process(doc);
		new SmbcVisaLoginChecker().check(out);
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException,UserProcessException {
		SmbcVisaGoDetail p = new SmbcVisaGoDetail(client,context);
		Document out = p.process(doc);
		return out;
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		SmbcVisaDownload p = new SmbcVisaDownload(client,context,outDir);
		File f = p.process(doc);
		return f;
	}

	@Override
	protected void logout(Document doc) throws IOException, UserProcessException {
		SmbcVisaLogout p = new SmbcVisaLogout(client,context);
		p.process(doc);
	}
	
	@Override
	protected void customClientBuilder(HttpClientBuilder builder) {
		RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
		builder.setDefaultRequestConfig(config);
	}
	@Override
	protected void handleUserProcessException(Exception upe) {
		if ( upe instanceof BillPendingException ) {
			log.log(Level.WARNING,"警告が発生しました。"+upe.getClass().getName()+":"+upe.getMessage());
			log.log(Level.WARNING,"中断します");
			setState(InternalState.WARNING);
		} else {
			super.handleUserProcessException(upe);
		}
	}

	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		Option out = new OutDirOption(false);options.addOption(out);
		options.addOption(OptionBuilder.hasArg(true).withArgName("userid")
				.isRequired(true).withDescription("ID")
				.create("userid"));
		Option password = new PasswordOption(true); options.addOption(password);
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		userId = cmd.getOptionValue("userid");
		this.password = cmd.getOptionValue(PasswordOption.OPT);
		outDir = new File(cmd.getOptionValue(OutDirOption.OPT, "."));
	}

}
